package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.LiuhecaiDetailActivity;
import com.lottery.ruok.lottrey.dialog.LiuhecaiTypeDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ASUS on 2017/11/13.
 */

public class LiuhecaiTypeAdapter extends BaseAdapter {
    private LiuhecaiDetailActivity liuhecaiDetailActivity;
    private LiuhecaiTypeDialog liuhecaiTypeDialog;
    private Context mContext;
    public LiuhecaiTypeAdapter(LiuhecaiDetailActivity liuhecaiDetailActivity, Context context, LiuhecaiTypeDialog liuhecaiTypeDialog){
        this.liuhecaiDetailActivity = liuhecaiDetailActivity;
        mContext = context;
        this.liuhecaiTypeDialog = liuhecaiTypeDialog;
    }

    @Override
    public int getCount() {
        if(liuhecaiDetailActivity.getLotteryDetailResponse == null){
            return 0;
        }
        return liuhecaiDetailActivity.getLotteryDetailResponse.getLayouts().size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.liuhecai_type_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.name.setText(liuhecaiDetailActivity.getLotteryDetailResponse.getLayouts().get(i).getName());
        if (liuhecaiDetailActivity.getLotteryDetailResponse.getLayouts().get(i).getSelected().equals("1")) {
            holder.name.setBackgroundResource(R.drawable.bg_corners_100_3);
            holder.name.setTextColor(mContext.getResources().getColor(R.color.bg_white));
        } else {
            holder.name.setBackgroundResource(R.drawable.bg_corners_100_12);
            holder.name.setTextColor(Color.parseColor("#3f3f3f"));
        }
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                liuhecaiTypeDialog.setAll();

                if (liuhecaiDetailActivity.getLotteryDetailResponse.getLayouts().get(i).getSelected().equals("1")) {
                    liuhecaiDetailActivity.getLotteryDetailResponse.getLayouts().get(i).setSelected("0");
                } else {
                    liuhecaiDetailActivity.getLotteryDetailResponse.getLayouts().get(i).setSelected("1");
                }
                notifyDataSetChanged();

                liuhecaiDetailActivity.chooseType(i);

                liuhecaiTypeDialog.getFragmentManager().popBackStack();
            }
        });

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.name)
        TextView name;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
